<?php
/**
 * 职位管理
 * @auth ray
*/
apf_require_class("Bll_BLL");
apf_require_class("Bll_Hr_Department");

class Bll_Hr_Job extends BLL
{
    /**
     * 获取单个职位信息
     * @param int $jobid
     */
    public function find_by_job_id ($jobid) {
        return $this->get_job_dao()->find_by_id($jobid);
    }
    /**
     * 获取多个个职位信息
     * @param unknown_type $ids
     * @param unknown_type $is_deleted
     * @return unknown|multitype:unknown
     */
    public function find_by_job_ids ($ids, $is_deleted = 1) {
        $data = array();
        if(empty($ids)){
            return $data;
        }
        $data = $this->get_job_dao()->find_by_ids($ids);
        if ($is_deleted == 3) {
            return $data;
        }
        $result = array();
        foreach ($data as $val) {
            if ($is_deleted == $val['is_deleted']) {
                $result[$val['job_id']] = $val;
            }
        }
        return $result;
    }

    /**
     * 需要所有的职位
     *
     * @param array $where
     */
    public function find_where ($where)
    {
        return $this->get_job_dao()->find_short_assoc($where, '', 0);
    }

    /**
     * 部门职位
     *
     * @param unknown_type $job_id
     */
    public function get_job_dep_info ($job_id)
    {
        $data = array();
        $job_data = $this->find_by_job_id($job_id);
        if (empty($job_data)) {
            return $data;
        }
        $dep_bll = new Bll_Hr_Department();
        $dep_data = $dep_bll->find_dep_by_id($job_data['department_id']);
        $data = array_merge($job_data, $dep_data);
        //编制人数
        $data['job_plan_num'] = $job_data['plan_num'];
        $data['job_fact_num'] = $job_data['fact_num'];
        $data['department_plan_num'] = $dep_data['plan_num'];
        $data['department_fact_num'] = $dep_data['fact_num'];
        return $data;
    }

    /**
     * 根据部门ID获取职位列表(job表)
     *
     * @param int|array $depid
     * @return multitype:
     */
    public function get_job_by_dep ($depid,$limit=0)
    {
        $return = array();
        if (! empty($depid)) {
            if (is_array($depid)) {
                $_where = array(
                        "department_id IN (" . implode(',', $depid) . ") AND 1=?" => 1
                );
            } else {
                $_where = array(
                        'department_id' => intval($depid)
                );
            }
            $_where['is_deleted'] = 1;
            $return = $this->get_job_dao()->find_short_assoc($_where, '', $limit);
        }
        return $return;
    }

    /**
     * 得到统计的数字
     */
    public function get_job_count ($where)
    {
        $bll = new Bll_Hr_Department();
        $dep_data = $bll->find_where($where);
        $count = 0;
        if (empty($dep_data)) {
            return $count;
        }
        $dep_ids = array();
        foreach ($dep_data as $val) {
            $dep_ids[] = $val['department_id'];
        }
        $_where["department_id IN(" . implode(',', $dep_ids) . ") AND 1=?"] = 1;
        $_where["is_deleted"] = 1;
        return $this->get_job_dao()->find_count($_where);
    }

    /**
     * 增加职位
     *
     * @param array $data
     * @return int
     */
    public function add_job ($data)
    {
        $job_id = 0;
        if (empty($data['department_id']) || empty($data['position_id'])) {
            return $job_id;
        }
        $job_id = $this->get_job_dao()->insert($data);
        return $job_id;
    }

    /**
     * 编辑职位计划编制
     *
     * @param int $job_id
     * @param int $plan_num
     * @return int boolen
     */
    public function update_plan_by_id ($job_id, $plan_num)
    {
        $status = false;
        if (empty($job_id)) {
            return $status;
        }
        $data = array(
                'plan_num' => intval($plan_num)
        );
        $status = $this->get_job_dao()->update_by_id(intval($job_id), $data);
        //计算部门职位人数
        $job_data = $this->find_by_job_id($job_id);
        apf_require_class("Bll_Hr_Department");
        $dep_bll = new Bll_Hr_Department();
        $plan_num = $this->sum_num($job_data['department_id']);
        $dep_bll->update_department($job_data['department_id'], array('plan_num'=>$plan_num));
        return $status;
    }
    public function sum_num($depid,$field='plan_num'){
        return $this->get_job_dao()->find_sum('plan_num',array('department_id'=>$depid));
    }
    /**
     * 编辑职位状态
     *
     * @param int $job_id
     * @param int $is_deleted
     *            1:正常 2：删除
     * @return int boolen
     */
    public function update_status_by_id ($job_id, $is_deleted)
    {
        $status = false;
        if (empty($job_id) || empty($is_deleted)) {
            return $status;
        }
        $data = array(
                'is_deleted' => intval($is_deleted)
        );
        $status = $this->get_job_dao()->update_by_id(intval($job_id), $data);
        return $status;
    }

    /**
     *
     * @param int $job_id
     * @param array $data
     * @param boolean $status
     *            $status为了做累加动作 $data = array('plan_num'=>'plan_num+1')
     */
    public function update_job_by_id ($job_id, $data, $status = false)
    {
        return $this->get_job_dao()->update_by_id(intval($job_id), $data,
                $status);
    }

    /**
     * 检测职位是否存在
     *
     * @param array $data
     * @return boolean true:已存在或参数错误
     */
    public function check_same_job ($data)
    {
        $status = true;
        if (empty($data['department_id']) || empty($data['position_id'])) {
            return $status;
        }
        $where = array(
                'department_id' => intval($data['department_id']),
                'position_id' => intval($data['position_id']),
                'is_deleted' => 1
        );
        $data = $this->get_job_dao()->find($where);
        if (empty($data)) {
            return false;
        }
        return $status;
    }

    public function get_function_position_relation ()
    {
        $department = array();
    }

    /**
     * 删除部门岗位信息
     *
     * @param int $dep_id
     * @return boolean
     */
    public function delete_job_by_department ($dep_id)
    {
        $status = false;
        if (empty($dep_id)) {
            return $status;
        }
        $status = $this->get_job_dao()->update(array(
                'is_deleted' => 2
        ), array(
                'department_id' => $dep_id
        ));
        return $status;
    }

    /**
     * 搜索职位信息
     *
     * @param string $job_name
     * @return multitype: multitype:unknown
     */
    public function search_job ($job_name)
    {
        $data = array();
        if (empty($job_name)) {
            return $data;
        }
        $sql = "SELECT * FROM  job WHERE position_name LIKE '%{$job_name}%' AND is_deleted=1";
        $tmp_pos_data = $this->get_job_dao()->execute($sql, array());
        if (empty($tmp_pos_data)) {
            return $data;
        }
        $dep_ids = array();
        foreach ($tmp_pos_data as $val) {
            $dep_ids[] = $val['department_id'];
        }
        $dep_ids = array_unique($dep_ids);
        // 得到部门信息
        apf_require_class("Bll_Hr_Department");
        $dep_bll = new Bll_Hr_Department();
        $depdata = $dep_bll->get_department_by_ids($dep_ids);
        // 格式化部门信息
        $depdata = $dep_bll->build_data($depdata);
        //
        foreach ($tmp_pos_data as $val) {
            $val['dep_info'] = $depdata[$val['department_id']];
            $data[$val['job_id']] = $val;
        }
        return $data;
    }
    /**
     *
     * @return Ambigous <Aifang_Core_DAO, NULL>
     */
    private function get_job_dao ()
    {
        $dao = $this->get_dao('Dao_Hr_Job');
        return $dao;
    }
}